package com.caimiao.miao.http.rxjava.exception;

import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.text.ParseException;

import retrofit2.HttpException;

public class LocalException extends Exception {
    /* 未知错误 */
    public static final int UNKNOWN = 1000;

    /* 解析错误 */
    public static final int PARSE_ERROR = 1001;

    /* 网络错误 */
    public static final int NETWORK_ERROR = 1002;

    /* 协议错误 */
    public static final int HTTP_ERROR = 1003;


    public static ApiException handleException(Throwable e) {
        if (e instanceof JSONException
                || e instanceof JsonParseException
                || e instanceof ParseException) {
            //解析错误
            return new ApiException(PARSE_ERROR, e.getMessage());
        } else if (e instanceof ConnectException
                || e instanceof UnknownHostException
                || e instanceof SocketTimeoutException) {
            //网络、连接错误
            return new ApiException(NETWORK_ERROR, e.getMessage());
        } else if (e instanceof HttpException) {
            return new ApiException(HTTP_ERROR, e.getMessage());
        } else {
            return new ApiException(UNKNOWN, e.getMessage());
        }
    }
}
